/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.meter;

import java.awt.BasicStroke;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author Administrator
 */
public class JNMeterChartPane extends JPanel{
    private TimeSeries inSeries;
    private TimeSeries outSeries;

    public JNMeterChartPane(int maxTime){
        super(new BorderLayout());
        String title = null;

        this.inSeries = new TimeSeries("Incoming Speed", Second.class);
        this.inSeries.setMaximumItemAge(maxTime);
        this.outSeries = new TimeSeries("Outgoing Speed", Second.class);
        this.outSeries.setMaximumItemAge(maxTime);
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(inSeries);
        dataset.addSeries(outSeries);

        DateAxis domain = new DateAxis();
        NumberAxis range = new NumberAxis();

        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        renderer.setSeriesPaint(0, Color.red);
        renderer.setSeriesPaint(1, Color.blue);
//        renderer.setSeriesStroke(0, new BasicStroke(3f, BasicStroke.CAP_BUTT,
//                BasicStroke.JOIN_BEVEL));
//        renderer.setSeriesStroke(1, new BasicStroke(3f, BasicStroke.CAP_BUTT,
//                BasicStroke.JOIN_BEVEL));
        //renderer.setBasePaint(Color.white);

        XYPlot plot = new XYPlot(dataset, domain, range, renderer);
        domain.setLowerMargin(0.0);
        range.setLowerMargin(0.0);
        domain.setTickLabelsVisible(false);
        domain.setAutoRange(true);
        range.setAutoRange(true);
        range.setTickLabelsVisible(false);

        

        JFreeChart chart = new JFreeChart(plot);
        chart.setPadding(RectangleInsets.ZERO_INSETS);
        chart.setTitle(title);
        chart.setBorderVisible(false);
        //chart.setBackgroundPaint(Color.white);
        renderer.setBaseSeriesVisibleInLegend(false);

        domain.setTickLabelInsets(RectangleInsets.ZERO_INSETS);
        range.setTickMarksVisible(false);
        range.setTickLabelInsets(RectangleInsets.ZERO_INSETS);
        domain.setTickMarksVisible(false);
        chart.setPadding(RectangleInsets.ZERO_INSETS);
        //ChartUtilities.applyCurrentTheme(chart);
        ChartPanel chartPanel = new ChartPanel(chart, true);
        chartPanel.setDisplayToolTips(false);
        chartPanel.setMouseZoomable(false, false);
        //chartPanel.setDomainZoomable(false);
        //chartPanel.setRangeZoomable(false);
        //chartPanel.setPreferredSize(new Dimension(20, 67));

        this.add(chartPanel, BorderLayout.CENTER);

    }

    public void addIncomingObservation(Double y){
        this.inSeries.addOrUpdate(new Second(), y);
    }

    public void addOutgoingObservation(Double y){
        this.outSeries.addOrUpdate(new Second(), y);
    }

    public void addObservation(Double x, Double y){
        this.inSeries.addOrUpdate(new Second(), x);
        this.outSeries.addOrUpdate(new Second(), y);
    }

}
